package controleur;

import Audio.AudioCapture;
import Network.ClientTCP;
import Network.autodiscover;
import View.MainFrame;
import View.ManageUserFrame;
import chataudio.Constant;
import java.io.IOException;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.List;
import model.AbstractChatModel;
import model.Connection;
import model.User;
import model.soundSetting;

/**
 *
 * @author Romain
 */
public abstract class AbstractChatControleur {
    protected AbstractChatModel model;
    ManageUserFrame userFrame;
    MainFrame mainWindow;
    autodiscover discover;
    
    public AbstractChatControleur(AbstractChatModel chat){
        this.model = chat;
        userFrame = new ManageUserFrame(this);
        userFrame.lancementApplication(); // traitement sepcifique pour le debut
        mainWindow = new MainFrame(this);
        addObserverToModel();
    }
    
    public void startAutoDiscover() throws IOException
    {
        discover = new autodiscover(this);
    }
    
    public void stopAutoDiscover() 
    {
        try {
        discover.arret();
        }
        catch(Exception e) { e.printStackTrace(); }
    }
    
    public void discover() throws UnknownHostException, IOException
    {
        discover.discover();
    }
    
    private void addObserverToModel(){
        model.addUserObserver(userFrame);
        model.addAtivityLineObserver(mainWindow);
        model.addUserObserver(mainWindow);
        model.addConnObserver(mainWindow);
    }
    
    public void displayMainFrame(){
        mainWindow.displayFrame();
    }
    
    public void displayUserFrame(){
        userFrame.displayFrame();
    }
    
    public soundSetting getLocalSoundSetting() {
        return this.model.getLocalSoundSetting();
    }
        
    
    public int getPortUDP() {
        return this.model.getPortUDP();                
    }
    
    public void setPortUDP(int port) {
        
        this.model.setPortUDP(port);
        
        ArrayList<Long> listID = this.model.getListConectionID();
        for (Long id : listID) 
        {
            ClientTCP client = new ClientTCP(this.model.getConnection(id), this);
            client.setPortUDP();
        }
    }
    
    public void AddConnToWaitList(Connection paramConn){
        this.model.AddConnToWaitList(paramConn);
    }
/*    
    public void AddConnToActList(Connection paramConn){
        this.model.AddConnToActList(paramConn);
    }
*/
    
    public void removeConnToWaitList(Connection paramConn){
        this.model.RemoveConnWaitList(paramConn);
    }
    
    public void removeConnToActiveList(Connection paramConn){
        this.model.RemoveConnActList(paramConn);
    }
    
    public List<User> GetUserList(){
        return this.model.getListUser();
    }
    
    public void addUserToList(String name)
    {
        this.model.addUserToList(name);
    }
    
    public void delUserToList(User u){
        this.model.delUserToList(u);
    }
    
    public boolean isEnableAutodiscover(){
        return this.model.isEnableAutodiscover();
    }
    
    public void disableAutodiscover(){
        this.model.disableAutodiscover();
    }
    
    public void enableAutodiscover(){
        this.model.enableAutodiscover();
    }
    
    public User getCurrentUser(){
        return this.model.getCurrentUser();
    }
    
    public void setCurrentUser(int u){
        this.model.setCurrentUser(u);
        //model.no
    }
    
    public void notifyConnectionChanged(){
        this.model.UpdateList();
    }
    
    public void createConnection(String host) throws UnknownHostException{
        Connection Conn = new Connection(model.getNewConnectionID(), host, getCurrentUser().getNom(), this);
        //this.model.AddConnToWaitList(Conn);
        ClientTCP cli = new ClientTCP(Conn, this);
        //cli.start();
        cli.nouveau();
    }
    
    public void stopAllActive()
    {
        model.stopAllActive();
        
    }
    
    public void disconectAllConnection()
    {    
        while (model.getFirstID() != 0) {
            ClientTCP cli = new ClientTCP(this.getConnection(model.getFirstID()), this);
            cli.setRefused();
            removeConnToWaitList(this.getConnection(model.getFirstID()));
            removeConnToActiveList(this.getConnection(model.getFirstID()));
        }
    }
    
    public void createAudioSession(Connection connection) throws UnknownHostException, SocketException{     
        this.model.AddConnToActList(connection);
        this.model.RemoveConnWaitList(connection);
        ClientTCP cli = new ClientTCP(connection, this);
        cli.setAccepted();
        connection.start();
    }
    
    public void stopAudioSession(Connection connection) throws UnknownHostException, SocketException{     
        connection.stop();
        this.model.RemoveConnActList(connection);
        this.mainWindow.stopChrono();
    }
    
    public Connection getActiveConnection(long ID)
    {
        return this.model.getActiveConnection(ID);
    }
    
    public Connection getConnection(long ID)
    {
        return this.model.getConnection(ID);
    }
    
    public String getWaitingID()
    {
        return this.model.getWaitingID();
    }
    
    public long getFirstActiveID()
    {
        return this.model.getFirstActiveID();
    }
    
    public Connection getWaitingConnection(long ID)
    {
        return this.model.getWaitingConnection(ID);
    }
    
    
    public void putConWaitToActiv(long ID)
    {
        this.model.putConWaitToActiv(ID);
    }
    
    public void putConActivToWait(long ID)
    {
        this.model.putConActivToWait(ID);
    }
    
    public void setUserToConn(long ID, String user)
    {
        this.model.setUserToConn(ID, user);
    }
    
    public void modifyConnectionStatus(long ID, Constant.Status status){
        this.model.modifyConnectionStatus(ID, status);
    }
    
    public void setManagerUserFrame(ManageUserFrame userFrame)
    {
        this.userFrame = userFrame;
        System.out.println("set userframe ");
    }
    
    public void hideManagerUserFrame()
    {
        try { 
        this.userFrame.hideFrame();
        } catch(Exception e) { System.out.println(e.toString() ); }
    }
      
    public void displayManagerUserFrame()
    {
        try { 
        this.userFrame.displayFrame();
        } catch(Exception e) { System.out.println(e.toString() ); }
        
    }
    
    public void setActivityLine(boolean activity){
        model.setLineActivity(activity);
    }
    
    public boolean getLineActivity(){
        return model.isLineActive();
    }
    
    public void startChrono(){
        this.mainWindow.startChrono();
    }
    public void stopChrono(){
        this.mainWindow.stopChrono();
    }        
    
    public void pausePlayer(){
        
    }
}
